{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Code：<a href=\"https://github.com/lotapp/BaseCode\" target=\"_blank\">https://github.com/lotapp/BaseCode</a>\n",
    "\n",
    "**多图旧版**：<a href=\"https://www.cnblogs.com/dunitian/p/9156097.html\" target=\"_blank\">https://www.cnblogs.com/dunitian/p/9156097.html</a>\n",
    "\n",
    "**在线预览**：<a href=\"http://github.lesschina.com/python/base/pop/3.list_tuple_dict_set.html\" target=\"_blank\">http://github.lesschina.com/python/base/pop/3.list_tuple_dict_set.html</a>\n",
    "\n",
    "今天说说`List、Tuple、Dict、Set`。POP部分还有一些如Func、IO（也可以放OOP部分说）然后就说说面向对象吧。\n",
    "\n",
    "先吐槽一下：Python面向对象真心需要规范，不然太容易走火入魔了 -_-!!! 汗，下次再说。。。\n",
    "\n",
    "## 1.Python列表相关\n",
    "\n",
    "### 1.1.列表定义、遍历\n",
    "\n",
    "`info_list=[]` #空列表\n",
    "\n",
    "`infos_list=[\"C#\",\"JavaScript\"]`\n",
    "\n",
    "遍历和之前一样，`for` 或者 `while` 都可以\n",
    "\n",
    "for扩展：<a href=\"https://www.cnblogs.com/dunitian/p/9103673.html#forelse\" target=\"_blank\">https://www.cnblogs.com/dunitian/p/9103673.html#forelse</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个列表，列表虽然可以存不同类型，一般我们把相同类型的值存列表里面\n",
    "infos_list=[\"C#\",\"JavaScript\"]#定一个空列表 list=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C#\n",
      "JavaScript\n"
     ]
    }
   ],
   "source": [
    "# for遍历\n",
    "for item in infos_list:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C#\n",
      "JavaScript\n"
     ]
    }
   ],
   "source": [
    "# while遍历\n",
    "i=0\n",
    "while i<len(infos_list):\n",
    "    print(infos_list[i])\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.列表添加\n",
    "\n",
    "**末尾追加** `infos_list.append(\"Java\")`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C#', 'JavaScript', 'Java']\n"
     ]
    }
   ],
   "source": [
    "# 添加~末尾追加\n",
    "infos_list.append(\"Java\")\n",
    "print(infos_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**指定位置插入** `infos_list.insert(0,\"Python\") `\n",
    "\n",
    "**插入列表** `infos_list.insert(0,temp_list)`\n",
    "\n",
    "**Python**在指定位置插入列表是真的`插入一个列表进去`，C#是把里面的元素挨个插入进去\n",
    "\n",
    "看后面的列表嵌套，是通过下标方式获取，eg: `infos_list[0][1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C#', 'JavaScript', 'Java']\n",
      "[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java']\n"
     ]
    }
   ],
   "source": [
    "# 添加～指定位置插入\n",
    "infos_list.insert(0,\"Python\")\n",
    "print(infos_list)\n",
    "\n",
    "# 列表嵌套（后面会有扩展）\n",
    "temp_list=[\"test1\",\"test2\"]\n",
    "infos_list.insert(0,temp_list)\n",
    "print(infos_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "infos_list #查看下现在列表是什么"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你想像C#那样把里面的元素挨个插入进去，可以用`extend()`\n",
    "\n",
    "**添加一个列表** `infos_list.extend(infos_list2)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三', 21]\n"
     ]
    }
   ],
   "source": [
    "# 添加一个列表\n",
    "infos_list2=[\"张三\",21]#python里面的列表类似于List<object>\n",
    "infos_list.extend(infos_list2)\n",
    "print(infos_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(...) method of builtins.list instance\n",
      "    L.extend(iterable) -> None -- extend list by appending elements from the iterable\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#可以查看extend方法描述\n",
    "help(infos_list.extend)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3.列表删除\n",
    "\n",
    "`infos_list.pop()` # 删除最后一个\n",
    "\n",
    "`infos_list.pop(0)` # 删除指定索引，不存在就报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除\n",
    "# pop()删除最后一个元素，返回删掉的元素\n",
    "infos_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['test1', 'test2'], 'Python', 'C#', 'JavaScript', 'Java', '张三']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "infos_list #查看一下列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['test1', 'test2']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除\n",
    "# pop(index) 删除指定下标元素，返回删掉的元素\n",
    "infos_list.pop(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Python', 'C#', 'JavaScript', 'Java', '张三']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "infos_list #查看一下列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "pop index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-ad52d76f39b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 索引不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0minfos_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: pop index out of range"
     ]
    }
   ],
   "source": [
    "# 索引不存在就报错\n",
    "infos_list.pop(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`infos_list.remove(\"张三\")` # `remove(\"\")`删除指定元素，不存在就报错\n",
    "\n",
    "`del infos_list[1]` # 删除指定下标元素，不存在就报错\n",
    "\n",
    "`del infos_list` # 删除集合（集合再访问就不存在了）不同于C#给集合赋null\n",
    "\n",
    "关于`del`的删除后面还会说，这个和`linux`里面的`ln`引用删除类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C#', 'JavaScript', 'Java']\n"
     ]
    }
   ],
   "source": [
    "# remove(\"\")删除指定元素\n",
    "infos_list.remove(\"张三\") #没有返回值\n",
    "print(infos_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-9f9cdd692e63>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "infos_list.remove(\"dnt\") # 不存在就报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'JavaScript', 'Java']\n"
     ]
    }
   ],
   "source": [
    "# del xxx[index] 删除指定下标元素\n",
    "del infos_list[1] #没有返回值\n",
    "print(infos_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list assignment index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-17-b6366d96a6e9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0minfos_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
     ]
    }
   ],
   "source": [
    "del infos_list[10] #不存在就报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "del infos_list # 删除集合（集合再访问就不存在了）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'infos_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-19-7de289d35755>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_list\u001b[0m \u001b[0;31m# 集合再访问就不存在了\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'infos_list' is not defined"
     ]
    }
   ],
   "source": [
    "infos_list # 集合再访问就不存在了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4.列表修改\n",
    "\n",
    "Python修改：（**只能通过索引修改**）\n",
    "\n",
    "`infos_list2[1]=\"PHP\"` # 只有下标修改一种方式，**不存在则异常**\n",
    "\n",
    "想按值修改需要**先查下标再修改** eg:\n",
    "\n",
    "`infos_list2.index(\"张三\")`\n",
    "\n",
    "`infos_list2[0]=\"GO\"`\n",
    "\n",
    "`infos_list2.index(\"dnt\")` # **不存在则异常**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 21]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改 xxx[index]=xx\n",
    "# 注意：一般不推荐在for循环里面修改\n",
    "infos_list2 #查看list2列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', 'PHP']\n"
     ]
    }
   ],
   "source": [
    "infos_list2[1]=\"PHP\" #只有下标修改一种方式\n",
    "print(infos_list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list assignment index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-22-ecf5fb72864d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_list2\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"GO\"\u001b[0m \u001b[0;31m#不存在则异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
     ]
    }
   ],
   "source": [
    "infos_list2[3]=\"GO\" #不存在则异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['GO', 'PHP']\n"
     ]
    }
   ],
   "source": [
    "# 想按值修改需要先查下标再修改\n",
    "infos_list2.index(\"张三\")\n",
    "infos_list2[0]=\"GO\"\n",
    "print(infos_list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "'dnt' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-24-6c57bb050f66>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_list2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#不存在则异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: 'dnt' is not in list"
     ]
    }
   ],
   "source": [
    "infos_list2.index(\"dnt\")#不存在则异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 知识面拓展： https://www.zhihu.com/question/49098374\n",
    "# 为什么python中不建议在for循环中修改列表？\n",
    "# 由于在遍历的过程中，删除了其中一个元素，导致后面的元素整体前移，导致有个元素成了漏网之鱼。\n",
    "# 同样的，在遍历过程中，使用插入操作，也会导致类似的错误。这也就是问题里说的无法“跟踪”元素。\n",
    "# 如果使用while，则可以在面对这样情况的时候灵活应对。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5.查询系列\n",
    "\n",
    "`in, not in, index, count`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询 in, not in, index, count\n",
    "names_list=[\"张三\",\"李四\",\"王二麻子\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['李四', '王二麻子']\n"
     ]
    }
   ],
   "source": [
    "# 张三在列表中执行操作\n",
    "if \"张三\" in names_list:\n",
    "    names_list.remove(\"张三\")\n",
    "print(names_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['李四', '王二麻子', '大舅子']\n"
     ]
    }
   ],
   "source": [
    "# 查看\"大舅子\"不在列表中执行操作\n",
    "if \"大舅子\" not in names_list:\n",
    "    names_list.append(\"大舅子\")\n",
    "print(names_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# 查询王二麻子的索引\n",
    "print(names_list.index(\"王二麻子\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 统计\n",
    "print(names_list.count(\"大舅子\")) \n",
    "print(names_list.count(\"逆天\")) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.6.排序系列\n",
    "\n",
    "`num_list.reverse() `# 倒序\n",
    "\n",
    "`num_list.sort()` # 从小到大排序\n",
    "\n",
    "`num_list.sort(reverse=True)` # 从大到小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 排序专用\n",
    "num_list=[1,3,5,88,7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 88, 5, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "# 倒序 reverse 逆置\n",
    "num_list.reverse()\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 88]\n"
     ]
    }
   ],
   "source": [
    "# 从小到大排序\n",
    "num_list.sort()\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88, 7, 5, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "# 从大到小\n",
    "num_list.sort(reverse=True)\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.7.列表切片\n",
    "\n",
    "列表的切片操作很有用，主要跟数据相关，实际应用中和dict（后面会讲）联合使用\n",
    "\n",
    "python切片语法：`[start_index:end_index:step]` （**end_index取不到**）\n",
    "\n",
    "先说说 `range`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# range扩展～创建一个整数列表\n",
    "# range(5)生成的序列是从0开始小于5的整数～[0,5)\n",
    "range_list=list(range(5))\n",
    "print(range_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# range(1,5)生成的序列是从1开始小于5的整数～[1,5)\n",
    "range_list=list(range(1,5))\n",
    "print(range_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
     ]
    }
   ],
   "source": [
    "# 列表的切片操作很有用，主要跟数据相关，实际应用中和dict（后面会讲）联合使用\n",
    "# python切片语法：[start_index:end_index:step] （end_index取不到）\n",
    "top100=list(range(1,101)) #[1,101) => 1~100\n",
    "print(top100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取前10个元素\n",
    "top100[:10] #等价于：top100[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取最后10个元素\n",
    "top100[-10:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 前11~20（eg：第二页）\n",
    "top100[10:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取80~90（eg:倒数第二页）\n",
    "top100[-20:-10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 前20个数，每两个取一个（eg:隔行换样式）\n",
    "top100[:20:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 所有数每10个取一个（eg:test的时候十里挑一）\n",
    "top100[::10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.8.Python列表相关的扩展\n",
    "\n",
    "**列表虽然可以存不同类型，一般我们把相同类型的值存列表里面，不同类型存字典里（key，value）**\n",
    "\n",
    "**列表嵌套**，获取用下标的方式：`num_list[5][1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88, 7, 5, 3, 1, [33, 44, 22]]\n"
     ]
    }
   ],
   "source": [
    "# #列表嵌套(列表也是可以嵌套的)\n",
    "num_list2=[33,44,22]\n",
    "num_list.append(num_list2)\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[33, 44, 22]\n",
      "44\n"
     ]
    }
   ],
   "source": [
    "# 输出\n",
    "print(num_list[5])\n",
    "print(num_list[5][1]) #嵌套列表获取值的方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# 引入Null==>None\n",
    "a=[1,2,3,4]\n",
    "b=[5,6]\n",
    "a=a.append(b)#a.append(b)没有返回值\n",
    "print(a)#None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**补充概念**，`str` 和 `tuple` 也可以用切片操作哦～\n",
    "\n",
    "str上次说了，这次说下Tuple（后面会继续说Tuple，先了解下吧）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取前两个 返回元组\n",
    "(1,2,3,4,5)[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.9.列表生成式\n",
    "\n",
    "**列表生成式**是Python内置用来 **创建list的生成式**\n",
    "\n",
    "eg：要生成 list `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`\n",
    "\n",
    "传统方法是通过循环来实现，比如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=1\n",
    "my_list=[]\n",
    "while(i<11):\n",
    "    my_list.append(i)\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有了列表生成式就方便了 `list(range(1, 11))`（之前说列表切片的时候稍微引入了一下range）\n",
    "\n",
    "另一种写法：`[x for x in range(1,11)]` 来看看案例： "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1, 11))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in range(1,11)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可能有人会问，第一种写法不是挺好的嘛，为什么要用第二种复杂写法？\n",
    "\n",
    "看看下面案例你就知道它的强大了（能简写就简单）\n",
    "\n",
    "现在有了`range`生成就更方便了，可如果我们需要 **1~10的平方列表**呢？`[1^2,2^2,....10^2]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "my_list=[]\n",
    "for i in range(1,11):\n",
    "    my_list.append(i*i)\n",
    "    i+=1\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是循环太繁琐，而列表生成式则可以用一行语句代替循环生成上面的list\n",
    "\n",
    "`[x * x for x in range(1, 11)]` 你可以这样理解==>**就是我们平时的for循环嘛，前面的参数是返回值罢了**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x*x for x in range(1,11)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把一个list中所有的字符串变成小写\n",
    "my_list = ['Hello', 'World', 'I', 'Love', 'You']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hello', 'world', 'i', 'love', 'you']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x.lower() for x in my_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表生成式的强大仅限于此嘛？No～\n",
    "\n",
    "**for循环后面还可以加上if判断** `[x for x in range(1, 11) if x % 2 == 0]`\n",
    "\n",
    "**多重for循环嵌套** `[x + y for x in 'ABC' for y in 'AB']`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8, 10]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1~10之间的偶数\n",
    "[x for x in range(1, 11) if x % 2 == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AA', 'AB', 'BA', 'BB', 'CA', 'CB']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数学里面的全排列\n",
    "[x + y for x in 'ABC' for y in 'AB']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其实你可以把他看成\n",
    "```py\n",
    "list1=[]\n",
    "for x in range(1,5):\n",
    "    for y in range(1,4):\n",
    "        list1.append((x,y))\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 1),\n",
       " (1, 2),\n",
       " (1, 3),\n",
       " (2, 1),\n",
       " (2, 2),\n",
       " (2, 3),\n",
       " (3, 1),\n",
       " (3, 2),\n",
       " (3, 3),\n",
       " (4, 1),\n",
       " (4, 2),\n",
       " (4, 3)]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数学里面的坐标轴（元组马上就讲了，你可以看看）\n",
    "[(x,y) for x in range(1,5) for y in range(1,4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 1, 1),\n",
       " (1, 1, 2),\n",
       " (1, 2, 1),\n",
       " (1, 2, 2),\n",
       " (1, 3, 1),\n",
       " (1, 3, 2),\n",
       " (2, 1, 1),\n",
       " (2, 1, 2),\n",
       " (2, 2, 1),\n",
       " (2, 2, 2),\n",
       " (2, 3, 1),\n",
       " (2, 3, 2),\n",
       " (3, 1, 1),\n",
       " (3, 1, 2),\n",
       " (3, 2, 1),\n",
       " (3, 2, 2),\n",
       " (3, 3, 1),\n",
       " (3, 3, 2),\n",
       " (4, 1, 1),\n",
       " (4, 1, 2),\n",
       " (4, 2, 1),\n",
       " (4, 2, 2),\n",
       " (4, 3, 1),\n",
       " (4, 3, 2)]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (x,y,z)\n",
    "[(x,y,z) for x in range(1,5) for y in range(1,4) for z in range(1,3)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.CSharp列表相关\n",
    "\n",
    "### 2.1.列表定义、遍历\n",
    "\n",
    "`var infos_list = new List<object>() { \"C#\", \"JavaScript\" };`\n",
    "\n",
    "遍历可以用`foreach，for，while`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C#\n",
      "JavaScript\n",
      "C#\n",
      "JavaScript\n",
      "C#\n",
      "JavaScript\n"
     ]
    }
   ],
   "source": [
    "%%script csharp\n",
    "//# 定义一个列表\n",
    "// # infos_list=[\"C#\",\"JavaScript\"]#[]\n",
    "var infos_list = new List<object>() { \"C#\", \"JavaScript\" };\n",
    "// // # ###########################################################\n",
    "// // # # 遍历 for while\n",
    "// // # for item in infos_list:\n",
    "// // #     print(item)\n",
    "foreach (var item in infos_list)\n",
    "{\n",
    "    System.Console.WriteLine(item);\n",
    "}\n",
    "for (int i = 0; i < infos_list.Count; i++)\n",
    "{\n",
    "    System.Console.WriteLine(infos_list[i]);\n",
    "}\n",
    "// # i=0\n",
    "// # while i<len(infos_list):\n",
    "// #     print(infos_list[i])\n",
    "// #     i+=1\n",
    "int j=0;\n",
    "while(j<infos_list.Count){\n",
    "   Console.WriteLine(infos_list[j++]);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2.列表添加\n",
    "\n",
    "`Add，AddRange，Insert，InsertRange` (和Python插入列表有些区别)\n",
    "\n",
    "为了后面演示的方便，我这边定义一个自定义输出：\n",
    "```csharp\n",
    "private static void DivPrintList(List<object> list, string say = \"\")\n",
    "{\n",
    "    Console.WriteLine($\"\\n{say}\");\n",
    "    foreach (var item in list)\n",
    "    {\n",
    "        System.Console.Write($\"{item} \");\n",
    "    }\n",
    "}\n",
    "```\n",
    "添加系列Code：\n",
    "```csharp\n",
    "var infos_list2 = new List<object>() { \"张三\", 21 };\n",
    "\n",
    "// # # 增加\n",
    "// # # 末尾追加\n",
    "// # infos_list.append(\"Java\")\n",
    "infos_list.Add(\"Java\");\n",
    "DivPrintList(infos_list);\n",
    "\n",
    "// # # 指定位置插入\n",
    "// # infos_list.insert(0,\"Python\")\n",
    "// # print(infos_list)\n",
    "infos_list.Insert(0,\"Python\");\n",
    "DivPrintList(infos_list);\n",
    "\n",
    "// # # 添加一个列表\n",
    "// # infos_list2=[\"张三\",21]#python里面的列表类似于List<object>            \n",
    "// # infos_list.extend(infos_list2)\n",
    "// # print(infos_list)\n",
    "infos_list.AddRange(infos_list2);\n",
    "DivPrintList(infos_list);\n",
    "\n",
    "/*C#有insertRange方法 */\n",
    "DivPrintList(infos_list2,\"List2原来的列表：\");\n",
    "infos_list2.InsertRange(0,infos_list);\n",
    "DivPrintList(infos_list2,\"List2变化后列表：\");\n",
    "```\n",
    "结果：\n",
    "```\n",
    "# 末尾追加\n",
    "C# JavaScript Java \n",
    "\n",
    "# 指定位置插入\n",
    "Python C# JavaScript Java \n",
    "\n",
    "# 添加一个列表\n",
    "Python C# JavaScript Java 张三 21 \n",
    "\n",
    "# insertRange方法\n",
    "List2原来的列表：\n",
    "张三 21 \n",
    "List2变化后列表：\n",
    "Python C# JavaScript Java 张三 21 张三 21 \n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3.列表删除\n",
    "\n",
    "**移除指定索引**：`infos_list.RemoveAt(1);`\n",
    "\n",
    "**移除指定值**： `infos_list.Remove(item);`\n",
    "\n",
    "**清空列表**： `infos_list.Clear();`\n",
    "\n",
    "```csharp\n",
    "infos_list.RemoveAt(1);\n",
    "// infos_list.RemoveAt(10);//不存在则报错\n",
    "// infos_list.RemoveRange(0,1); //可以移除多个\n",
    "DivPrintList(infos_list);\n",
    "infos_list.Remove(\"我家在东北吗？\"); //移除指定item，不存在不会报错\n",
    "DivPrintList(infos_list,\"清空前：\");\n",
    "infos_list.Clear();//清空列表\n",
    "DivPrintList(infos_list,\"清空后：\");\n",
    "```\n",
    "输出：\n",
    "```\n",
    "Python JavaScript Java 张三 21 \n",
    "清空前：\n",
    "Python JavaScript Java 张三 21 \n",
    "清空后：\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4.列表修改\n",
    "\n",
    "基本上和Python一样\n",
    "```csharp\n",
    "DivPrintList(infos_list2);\n",
    "infos_list2[1] = \"PHP\";\n",
    "// infos_list2[3]=\"GO\"; //不存在则异常\n",
    "DivPrintList(infos_list2);\n",
    "// # # 想按值修改需要先查下标再修改\n",
    "// # infos_list2.index(\"张三\")\n",
    "// # infos_list2[0]=\"GO\"\n",
    "// # print(infos_list2)\n",
    "// # # infos_list2.index(\"dnt\")#不存在则异常\n",
    "int index = infos_list2.IndexOf(\"张三\");\n",
    "infos_list2[index] = \"GO\";\n",
    "DivPrintList(infos_list2);\n",
    "infos_list2.IndexOf(\"dnt\");//不存在返回-1\n",
    "```\n",
    "输出：\n",
    "```\n",
    "Python C# JavaScript Java 张三 21 张三 21 \n",
    "Python PHP JavaScript Java 张三 21 张三 21 \n",
    "Python PHP JavaScript Java GO 21 张三 21\n",
    "```\n",
    "\n",
    "### 2.5.列表查询\n",
    "\n",
    "`IndexOf`，`Count` 这两个讲过了\n",
    "\n",
    "查找用`Contains`，其他的用法你可以先看看\n",
    "```\n",
    "// # 查询 in, not in, index, count\n",
    "// # names_list=[\"张三\",\"李四\",\"王二麻子\"]\n",
    "var names_list=new List<string>(){\"张三\",\"李四\",\"王二麻子\"};\n",
    "// Console.WriteLine(names_list.Find(i=>i==\"张三\"));\n",
    "// Console.WriteLine(names_list.FirstOrDefault(i=>i==\"张三\"));\n",
    "Console.WriteLine(names_list.Exists(i=>i==\"张三\"));\n",
    "Console.WriteLine(names_list.Contains(\"张三\"));\n",
    "```\n",
    "结果：\n",
    "```\n",
    "True\n",
    "True\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.列表排序\n",
    "\n",
    "```csharp\n",
    "// # # 排序(sort, reverse 逆置)\n",
    "// # num_list=[1,3,5,88,7]\n",
    "var num_list = new List<object>() { 1, 3, 5, 88, 7 };\n",
    "\n",
    "// # #倒序\n",
    "// # num_list.reverse()\n",
    "// # print(num_list)\n",
    "num_list.Reverse();\n",
    "DivPrintList(num_list);\n",
    "// # # 从小到大排序\n",
    "// # num_list.sort()\n",
    "// # print(num_list)\n",
    "num_list.Sort();\n",
    "DivPrintList(num_list);\n",
    "\n",
    "// # # 从大到小\n",
    "// # num_list.sort(reverse=True)\n",
    "// # print(num_list)\n",
    "num_list.Sort();\n",
    "num_list.Reverse();\n",
    "DivPrintList(num_list);\n",
    "```\n",
    "输出：\n",
    "```\n",
    "7 88 5 3 1 \n",
    "1 3 5 7 88 \n",
    "88 7 5 3 1 \n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "### 2.7.列表嵌套和多维数组的扩展\n",
    "\n",
    "列表嵌套不能像python那样 **下标操作**，你可以**继续循环遍历**，或者可以定义**多维数组**来支持 `num_list2[i][j]`\n",
    "\n",
    "定义：`var num_list2 = new List<object>() { 33, 44, 22,new List<object>(){11,55,77} };`\n",
    "\n",
    "关于多维数组的案例可以看我以前讲解的Code：<a href=\"https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/1.面向过程/02.数组系列\" target=\"_blank\">https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/1.面向过程/02.数组系列</a>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.Python元组相关\n",
    "\n",
    "### 3.1.元组定义、遍历等\n",
    "\n",
    "定义：`xxx=(xxx,xxx,xxx)`\n",
    "\n",
    "定义一个元素的元组：`xxx=(1,)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只能查询，其他操作和列表差不多（不可变）(最后面有可变扩展)\n",
    "test_tuple=(\"萌萌哒\",1,3,5,\"加息\",\"加息\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "# 定义的扩展：\n",
    "test_tuple1=(1,) #(1)就不是元祖了\n",
    "test_tuple2=(2)\n",
    "print(type(test_tuple1))\n",
    "print(type(test_tuple2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# count index\n",
    "print(test_tuple.count(\"加息\"))\n",
    "print(test_tuple.index(\"萌萌哒\"))#没有find方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "tuple.index(x): x not in tuple",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-64-293cf803dc90>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 从特定位置查找，注意是左闭右开区间==>[1,4)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"加息\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;31m#查不到报错：ValueError: tuple.index(x): x not in tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: tuple.index(x): x not in tuple"
     ]
    }
   ],
   "source": [
    "# 从特定位置查找，注意是左闭右开区间==>[1,4)\n",
    "print(test_tuple.index(\"加息\", 1, 4))#查不到报错：ValueError: tuple.index(x): x not in tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "#下标取\n",
    "print(test_tuple[0])\n",
    "print(test_tuple[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "1\n",
      "3\n",
      "5\n",
      "加息\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "# 遍历方式1\n",
    "for item in test_tuple:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "萌萌哒\n",
      "1\n",
      "3\n",
      "5\n",
      "加息\n",
      "加息\n"
     ]
    }
   ],
   "source": [
    "# 遍历方式2\n",
    "i=0\n",
    "while i<len(test_tuple):\n",
    "    print(test_tuple[i])\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.拆包、多维元组\n",
    "\n",
    "先来说说 **拆包**相关的知识\n",
    "\n",
    "`a=(1,2)`\n",
    "\n",
    "`b=a` # 把a的引用给b\n",
    "\n",
    "`c,d=a` # 不是把a分别赋值给c和d，等价于：`c=a[0] d=a[1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2)\n",
      "(1, 2)\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 后面讲字典遍历的时候会再提一下的\n",
    "a=(1,2)\n",
    "b=a#把a的引用给b\n",
    "#a里面两个值,直接给左边两个变量赋值了（有点像拆包了）\n",
    "c,d=a #不是把a分别赋值给c和d，等价于：c=a[0] d=a[1]\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# 交换两数～元组的方式\n",
    "a=1\n",
    "b=2\n",
    "a,b=b,a # 写全：(a,b)=(b,a)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**多维元组**\n",
    "\n",
    "`some_tuples=[(2,\"萌萌哒\"),(4,3)]`\n",
    "\n",
    "`some_tuples[0]`\n",
    "\n",
    "`some_tuples[0][1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'萌萌哒'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维元组\n",
    "some_tuples=[(2,\"萌萌哒\"),(4,3)]\n",
    "some_tuples[0]\n",
    "some_tuples[0][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3.可变元组\n",
    "\n",
    "**可变的元组**（元组在定义的时候就不能变了，但是可以通过类似这种方式来改变）\n",
    "\n",
    "案例里面用到了列表和字典（本章有讲解，这边你先看看）\n",
    "\n",
    "参照C#的可变元组会更容易懂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 扩展：可变的元组（元组在定义的时候就不能变了，但是可以通过类似这种方式来改变）\n",
    "value_tuple = (\"a\", \"1\", [\"mmd\"],{\"name\":\"dnt\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', '1', ['mmd'], {'name': 'dnt'})"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value_tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', '1', ['mmd', 'test'], {'name': 'dnt'})\n"
     ]
    }
   ],
   "source": [
    "value_tuple[2].append(\"test\")\n",
    "print(value_tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', '1', ['mmd', 'test'], {'name': 'dnt', 'wechat': 'dotnetcrazy'})\n"
     ]
    }
   ],
   "source": [
    "value_tuple[3][\"wechat\"]=\"dotnetcrazy\"\n",
    "print(value_tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.CSharp元组相关\n",
    "\n",
    "逆天`ValueTuple`用的比较多，下面案例就是用的这个\n",
    "\n",
    "**元组系**:<a href=\"https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx\" target=\"_blank\">https://msdn.microsoft.com/zh-cn/library/system.tuple.aspx</a>\n",
    "\n",
    "**值元组**:<a href=\"https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx\" target=\"_blank\">https://msdn.microsoft.com/zh-cn/library/system.valuetuple.aspx</a>\n",
    " \n",
    "C#中元组主要是方便程序员,不用自然可以。比如:当你返回多个值是否还用`ref` `out` 或者返回一个`list`之类的？ \n",
    "\n",
    "这些都需要先定义,比较麻烦.元祖在这些场景用的比较多。\n",
    "\n",
    "先说说基本使用：\n",
    "\n",
    "初始化：`var test_tuple = (\"萌萌哒\", 1, 3, 5, \"加息\", \"加息\"); `\n",
    "\n",
    "这种方式就是valueTuple了（看vscode监视信息）\n",
    "\n",
    "![图片](https://images2018.cnblogs.com/blog/1127869/201806/1127869-20180608150317914-1109684655.png)\n",
    "\n",
    "```csharp\n",
    "// 初始化\n",
    "var test_tuple = (\"萌萌哒\", 1, 3, 5, \"加息\", \"加息\"); //这种方式就是valueTuple了\n",
    "test_tuple.Item1 = \"ddd\";//可以修改值\n",
    "test_tuple.GetType();\n",
    "```\n",
    "\n",
    "需要说下的是，取值只能通过**itemxxx**来取了，然后就是**`valueTuple`的值是可以修改的**\n",
    "\n",
    "![图片](https://images2018.cnblogs.com/blog/1127869/201806/1127869-20180608150525883-160764614.png)\n",
    "\n",
    "下面直接进入应用场景：\n",
    "```csharp\n",
    "var result = GetCityAndTel();  //支持async/await模式\n",
    "var city = result.city;\n",
    "var tel = result.tel;\n",
    "// 拆包方式:\n",
    "var (city1, tel1) = GetCityAndTel();\n",
    "```\n",
    "贴一下方法：\n",
    "```csharp\n",
    "// public static (string city, string tel) GetCityAndTel()\n",
    "// {\n",
    "//     return (\"北京\", \"110\");\n",
    "// }\n",
    "// 简化写法\n",
    "public static (string city, string tel) GetCityAndTel() => (\"北京\", \"110\");\n",
    "```\n",
    "再说一下，C#元组的方式交换两数：\n",
    "```csharp\n",
    "int x = 1, y = 2;\n",
    "(x, y) = (y, x);\n",
    "Console.WriteLine(\"x： \" + x + \"  y： \" + x);\n",
    "```\n",
    "PS：附上Python进行对比记忆：\n",
    "```py\n",
    "a=1\n",
    "b=2\n",
    "a,b=b,a # 写全：(a,b)=(b,a)\n",
    "```\n",
    "就说到这了，简单了解即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.Python字典系列\n",
    "\n",
    "### 5.1.字典定义、遍历\n",
    "\n",
    "主要解析一下这个：\n",
    "```py\n",
    "for k,v in infos_dict.items():\n",
    "　　print(\"Key:%s,Value:%s\"%(k,v))\n",
    "```\n",
    "每一次相当于取一个**元组**，那可以用之前讲的例子来简化了：`c,d=a` 等价于：`c=a[0] d=a[1]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "infos_dict={\"name\":\"dnt\",\"web\":\"dkill.net\"} #空字典定义 dict={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "web\n"
     ]
    }
   ],
   "source": [
    "# 遍历keys\n",
    "for item in infos_dict.keys():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "web\n"
     ]
    }
   ],
   "source": [
    "#注意，如果你直接对infos遍历，其实只是遍历keys\n",
    "for item in infos_dict:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dnt\n",
      "dkill.net\n"
     ]
    }
   ],
   "source": [
    "# 遍历values\n",
    "for item in infos_dict.values():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Key:name,Value:dnt\n",
      "Key:web,Value:dkill.net\n"
     ]
    }
   ],
   "source": [
    "# 遍历键值对\n",
    "for item in infos_dict.items():\n",
    "    print(\"Key:%s,Value:%s\"%(item[0],item[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Key:name,Value:dnt\n",
      "Key:web,Value:dkill.net\n"
     ]
    }
   ],
   "source": [
    "# 每一次相当于取一个元组，那可以用之前讲的例子来简化了：c,d=a #等价于：c=a[0] d=a[1]\n",
    "for k,v in infos_dict.items():\n",
    "    print(\"Key:%s,Value:%s\"%(k,v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['name:dnt', 'web:dkill.net']"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 活学活用，用列表生成式列表\n",
    "[k + ':' + v for k,v in infos_dict.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.增加和修改\n",
    "\n",
    "**增加、修改**：`infos_dict[\"wechat\"]=\"dotnetcrazy\"` # 有就修改，没就添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'dnt', 'web': 'dkill.net', 'wechat': 'lll'}\n",
      "{'name': 'dnt', 'web': 'dkill.net', 'wechat': 'dotnetcrazy'}\n"
     ]
    }
   ],
   "source": [
    "# 增加 修改 (有就修改，没就添加)\n",
    "# 添加\n",
    "infos_dict[\"wechat\"]=\"lll\"\n",
    "print(infos_dict)\n",
    "\n",
    "# 修改\n",
    "infos_dict[\"wechat\"]=\"dotnetcrazy\"\n",
    "print(infos_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**补充**：dict内部存放的顺序和key放入的顺序是没有关系的\n",
    "\n",
    "dict的key必须是 **不可变对象**，dict根据key进行hash算法,来计算value的存储位置\n",
    "\n",
    "如果每次计算相同的key得出的结果不同，那dict内部就完全混乱了\n",
    "\n",
    "测试结果：**元组是可以作为Key的**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dict的key必须是不可变对象的验证案例\n",
    "key1=(1,2,3)\n",
    "key2=[1,2,3]\n",
    "key3={\"1\":\"2\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "dic={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元组是不可变类型，可以当key\n",
    "dic[key1]=\"mmd\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-86-f185376d67c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# dict根据key进行hash算法,来计算value的存储位置\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;31m# 如果每次计算相同的key得出的结果不同，那dict内部就完全混乱了\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdic\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"dnt\"\u001b[0m \u001b[0;31m# unhashable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "# dict根据key进行hash算法,来计算value的存储位置\n",
    "# 如果每次计算相同的key得出的结果不同，那dict内部就完全混乱了\n",
    "dic[key2]=\"dnt\" # unhashable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'dict'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-87-3f776e9bf984>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 字典也不行\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdic\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"test\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'dict'"
     ]
    }
   ],
   "source": [
    "# 字典也不行\n",
    "dic[key3]=\"test\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3.删除\n",
    "\n",
    "**删除系列**：\n",
    "\n",
    "清空字典内容 `infos_dict.clear()`\n",
    "\n",
    "删除指定内容 `del infos_dict[\"name\"]`（**没有返回值**） or `pop(key)`（**返回删除Key的值**） 不存在都会报错\n",
    "\n",
    "删除字典 `del infos_dict`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'dnt', 'web': 'dkill.net', 'wechat': 'dotnetcrazy'}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "infos_dict #查看列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dotnetcrazy'"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 要删除一个key，用pop(key)方法，对应的value也会从dict中删除\n",
    "infos_dict.pop(\"wechat\") #返回key对应的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'wechat'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-90-dc5eeda55ffa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"wechat\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#key不存在，则报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'wechat'"
     ]
    }
   ],
   "source": [
    "infos_dict.pop(\"wechat\") #key不存在，则报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'web': 'dkill.net'}\n"
     ]
    }
   ],
   "source": [
    "del infos_dict[\"name\"] #没有返回值\n",
    "print(infos_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-92-2a26e199752e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0minfos_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"name\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#不存在就报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'name'"
     ]
    }
   ],
   "source": [
    "del infos_dict[\"name\"] #不存在就报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "#清空字典内容\n",
    "infos_dict.clear()\n",
    "print(infos_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除字典\n",
    "del infos_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.4.查询\n",
    "\n",
    "**查询系列**：推荐：`infos_dict.get(\"mmd\")` # 查不到不会异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "infos_dict={\"name\":\"dnt\",\"web\":\"dkill.net\"} #刚才被删掉了，我们重新定义一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dnt'"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "infos_dict[\"name\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'mmd'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-97-bc0a122c60bb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minfos_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"mmd\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#查不到就异常\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'mmd'"
     ]
    }
   ],
   "source": [
    "infos_dict[\"mmd\"] #查不到就异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# 要避免key不存在的错误，有两种办法\n",
    "# 一是通过in判断key是否存在：\n",
    "print(\"mmd\" in infos_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# 二是通过dict提供的get()方法\n",
    "infos_dict.get(\"name\")\n",
    "print(infos_dict.get(\"mmd\"))#如果key不存在，返回None\n",
    "print(infos_dict.get(\"mmd\",-1))#也可以返回自己指定的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看帮助\n",
    "# help(infos_dict)\n",
    "len(infos_dict) #有几对key,value \n",
    "# infos_dict.has_key(\"name\") #这个是python2里面的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.CSharp字典系列\n",
    "\n",
    "### 6.1.定义、遍历\n",
    "\n",
    "C#的字典操作大家比较熟悉了，而且挺简单的，就一笔带过了\n",
    "\n",
    "```csharp\n",
    "//定义\n",
    "var infos_dict = new Dictionary<string, object>{\n",
    "                {\"name\",\"dnt\"},\n",
    "                {\"web\",\"dkill.net\"}\n",
    "            };\n",
    "//遍历\n",
    "foreach (KeyValuePair<string, object> kv in infos_dict)\n",
    "{\n",
    "　　Console.WriteLine($\"Key:{kv.Key},Value:{kv.Value}\");\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2.增删改查\n",
    "\n",
    "```csharp\n",
    "//添加\n",
    "infos_dict.Add(\"wechat\", \"lll\");\n",
    "infos_dict[\"wechat1\"] = \"lll\";\n",
    "\n",
    "//修改\n",
    "infos_dict[\"wechat\"] = \"dotnetcrazy\";\n",
    "```\n",
    "删除系列：\n",
    "\n",
    "```csharp\n",
    "// 删除元素\n",
    "// # del infos_dict[\"name\"]\n",
    "// # del infos_dict[\"dog\"] #不存在就报错\n",
    "// # print(infos_dict)\n",
    "infos_dict.Remove(\"name\");\n",
    "infos_dict.Remove(\"dog\");//不存在不报错\n",
    "// 清空列表内容\n",
    "// # infos_dict.clear()\n",
    "// # print(infos_dict)\n",
    "infos_dict.Clear();\n",
    "```\n",
    "查询系列：\n",
    "```csharp\n",
    "// infos_dict[\"name\"]\n",
    "// infos_dict[\"mmd\"] #查不到就异常            \n",
    "// infos_dict.get(\"name\")\n",
    "// infos_dict.get(\"mmd\")#查不到不会异常\n",
    "Console.WriteLine(infos_dict[\"name\"]);\n",
    "// Console.WriteLine(infos_dict[\"mmd\"]); //#查不到就异常\n",
    "// 先看看有没有 ContainsKey(key),看值就 ContainsValue(value)\n",
    "if (infos_dict.ContainsKey(\"mmd\")) Console.WriteLine(infos_dict[\"mmd\"]);\n",
    "\n",
    "// len(infos_dict) #有几对key,value\n",
    "Console.WriteLine(infos_dict.Count);\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.集合Set\n",
    "\n",
    "### 7.1.定义、遍历\n",
    "\n",
    "定义：`set(iterable)` \n",
    "\n",
    "eg:`set([1,2,1,\"mmd\"])` 基本上能for循环的都可以（`list，tuple，dict，str`）\n",
    "\n",
    "如果是字符串，则拆分成单个字符集合 `set(\"abc\")`\n",
    "\n",
    "集合Set注意个东西：（list去重一般就和set结合使用）\n",
    "\n",
    "**重复元素在自动被过滤**(数学里面的集合也是没有重复元素的)\n",
    "\n",
    "遍历：\n",
    "```py\n",
    "for item in my_set:\n",
    "    print(item)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on class set in module builtins:\n",
      "\n",
      "class set(object)\n",
      " |  set() -> new empty set object\n",
      " |  set(iterable) -> new set object\n",
      " |  \n",
      " |  Build an unordered collection of unique elements.\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __and__(self, value, /)\n",
      " |      Return self&value.\n",
      " |  \n",
      " |  __contains__(...)\n",
      " |      x.__contains__(y) <==> y in x.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __iand__(self, value, /)\n",
      " |      Return self&=value.\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __ior__(self, value, /)\n",
      " |      Return self|=value.\n",
      " |  \n",
      " |  __isub__(self, value, /)\n",
      " |      Return self-=value.\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __ixor__(self, value, /)\n",
      " |      Return self^=value.\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  __or__(self, value, /)\n",
      " |      Return self|value.\n",
      " |  \n",
      " |  __rand__(self, value, /)\n",
      " |      Return value&self.\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Return state information for pickling.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __ror__(self, value, /)\n",
      " |      Return value|self.\n",
      " |  \n",
      " |  __rsub__(self, value, /)\n",
      " |      Return value-self.\n",
      " |  \n",
      " |  __rxor__(self, value, /)\n",
      " |      Return value^self.\n",
      " |  \n",
      " |  __sizeof__(...)\n",
      " |      S.__sizeof__() -> size of S in memory, in bytes\n",
      " |  \n",
      " |  __sub__(self, value, /)\n",
      " |      Return self-value.\n",
      " |  \n",
      " |  __xor__(self, value, /)\n",
      " |      Return self^value.\n",
      " |  \n",
      " |  add(...)\n",
      " |      Add an element to a set.\n",
      " |      \n",
      " |      This has no effect if the element is already present.\n",
      " |  \n",
      " |  clear(...)\n",
      " |      Remove all elements from this set.\n",
      " |  \n",
      " |  copy(...)\n",
      " |      Return a shallow copy of a set.\n",
      " |  \n",
      " |  difference(...)\n",
      " |      Return the difference of two or more sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in this set but not the others.)\n",
      " |  \n",
      " |  difference_update(...)\n",
      " |      Remove all elements of another set from this set.\n",
      " |  \n",
      " |  discard(...)\n",
      " |      Remove an element from a set if it is a member.\n",
      " |      \n",
      " |      If the element is not a member, do nothing.\n",
      " |  \n",
      " |  intersection(...)\n",
      " |      Return the intersection of two sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in both sets.)\n",
      " |  \n",
      " |  intersection_update(...)\n",
      " |      Update a set with the intersection of itself and another.\n",
      " |  \n",
      " |  isdisjoint(...)\n",
      " |      Return True if two sets have a null intersection.\n",
      " |  \n",
      " |  issubset(...)\n",
      " |      Report whether another set contains this set.\n",
      " |  \n",
      " |  issuperset(...)\n",
      " |      Report whether this set contains another set.\n",
      " |  \n",
      " |  pop(...)\n",
      " |      Remove and return an arbitrary set element.\n",
      " |      Raises KeyError if the set is empty.\n",
      " |  \n",
      " |  remove(...)\n",
      " |      Remove an element from a set; it must be a member.\n",
      " |      \n",
      " |      If the element is not a member, raise a KeyError.\n",
      " |  \n",
      " |  symmetric_difference(...)\n",
      " |      Return the symmetric difference of two sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in exactly one of the sets.)\n",
      " |  \n",
      " |  symmetric_difference_update(...)\n",
      " |      Update a set with the symmetric difference of itself and another.\n",
      " |  \n",
      " |  union(...)\n",
      " |      Return the union of sets as a new set.\n",
      " |      \n",
      " |      (i.e. all elements that are in either set.)\n",
      " |  \n",
      " |  update(...)\n",
      " |      Update a set with the union of itself and others.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  __hash__ = None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 先看个帮助文档\n",
    "help(set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个set集合\n",
    "# set(iterable) -> new set object #列表就比较合适了\n",
    "my_set=set([1,2,1,\"mmd\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 'mmd'}"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数学里面也是用大括号表示的\n",
    "my_set # 重复元素在自动被过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_set=set((1,2,3,3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只会存不重复的key值\n",
    "my_set=set({\"name\":\"mmd\",\"name\":\"ddd\",\"age\":22})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'age', 'name'}"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age\n",
      "name\n"
     ]
    }
   ],
   "source": [
    "# 遍历 my_set\n",
    "for item in my_set:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 33, 22, 111]\n"
     ]
    }
   ],
   "source": [
    "# list去重案例：\n",
    "my_list=[1,111,22,33,1,1,1]\n",
    "my_list=list(set(my_list))\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.2.增删改系列\n",
    "\n",
    "添加元素：\n",
    "\n",
    "`add()` 添加一个元素\n",
    "\n",
    "`update()` 添加一些元素\n",
    "\n",
    "删除系列：\n",
    "\n",
    "`discard()` 有就删除，没有不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'add', 'age', 'name'}\n"
     ]
    }
   ],
   "source": [
    "# 添加元素\n",
    "my_set.add(\"add\") #没有返回值\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 'age', 'name', 'add'}\n"
     ]
    }
   ],
   "source": [
    "# 添加一些元素\n",
    "my_set.update([1,4,3])\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 6, 7, 9, 'age', 'name', 'add'}\n"
     ]
    }
   ],
   "source": [
    "my_set.update((6,7,9))\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 'o', 3, 4, 6, 7, 'L', 9, 'age', 'v', 'name', 'add', 'e'}\n"
     ]
    }
   ],
   "source": [
    "# 字符串被拆成字符存储\n",
    "my_set.update(\"Love\")\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "################### 删除系列 ###########################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'mmd'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-115-1d51a9949e6e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 删除元素\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmy_set\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"mmd\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 不存在则报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_set\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'mmd'"
     ]
    }
   ],
   "source": [
    "# 删除元素\n",
    "my_set.remove(\"mmd\") # 不存在则报错\n",
    "print(my_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除 name\n",
    "my_set.remove(\"name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 6, 7, 9, 'L', 'add', 'age', 'e', 'o', 'v'}"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop删除\n",
    "# pop一般不用，说法不一，有些说删除第一个有些说随机\n",
    "# 了解就好了，不用管pop（全数字的时候，我测试的确删的是第一个）\n",
    "my_set.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4, 6, 7, 9, 'L', 'add', 'age', 'e', 'o', 'v'}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 清空\n",
    "my_set.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有就删除，没有也不会报错\n",
    "my_set.discard(\"dnt\") # 没有返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.3.交、并、差、子集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 5}\n",
      "{2, 4, 6}\n"
     ]
    }
   ],
   "source": [
    "#利用运算符+set 实现数学方面的扩展\n",
    "set1=set([1,2,5])\n",
    "set2=set([2,4,6])\n",
    "\n",
    "print(set1)\n",
    "print(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2}"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集 A∩B={x|x∈A,且x∈B}\n",
    "set1 & set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6}"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集 A∪B={x|x∈A,或x∈B}\n",
    "set1 | set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 5}"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集 A-B={x∣x∈A,且x∉B}\n",
    "set1 - set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 4, 5, 6}"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对称差集（互相没有的取出来）\n",
    "set1^set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2}"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Set方法实现交集\n",
    "set1.intersection(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 4, 5, 6}"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Set方法去重后的并集\n",
    "set1.union(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 5}"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集（把set1里面有的而set2里面没有的取出）\n",
    "set1.difference(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 4, 5, 6}"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对称差集（互相没有的取出来）\n",
    "set1.symmetric_difference(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 再定义两个Set用来进行下面调试\n",
    "set3=set([1,2])\n",
    "set4=set([7,8,9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 子集（判断set3是否是set1的子集）\n",
    "set3.issubset(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 父集（set1是否是set3的父集）\n",
    "set1.issuperset(set3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断两个集合是否没有交集\n",
    "set1.isdisjoint(set4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反过来也一样\n",
    "set4.isdisjoint(set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "################### 补集的扩展 ###########################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "# 补集\n",
    "set3=set(list(range(10)))\n",
    "\n",
    "print(set3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 【大前提】set2是set3的一个子集（set3包含于set2）\n",
    "set2.issubset(set3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 5, 7, 8, 9}"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这时候求差集，就等于求补集\n",
    "set3 - set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 其他内容可以直接查看help"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.Python扩展\n",
    "\n",
    "### 8.1.运算符扩展\n",
    "\n",
    "`+` 合并，`*` 复制，`in` 是否存在（**字典是查key**），not in 是否不存在（**字典是查key**）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_str=\"www.baidu.com\"\n",
    "test_list=[1,\"d\",5]\n",
    "test_list1=[2,4,\"n\",\"t\",3]\n",
    "test_dict={\"name\":\"dnt\",\"wechat\":\"xxx\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "www.baidu.comwww.baidu.com\n",
      "[1, 'd', 5, 2, 4, 'n', 't', 3]\n"
     ]
    }
   ],
   "source": [
    "# + 合并 (不支持字典)\n",
    "print(test_str+test_str)\n",
    "print(test_list+test_list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "www.baidu.comwww.baidu.com\n",
      "[1, 'd', 5, 1, 'd', 5]\n"
     ]
    }
   ],
   "source": [
    "# * 复制 (不支持字典)\n",
    "print(test_str*2)\n",
    "print(test_list*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# in 是否存在（字典是查key）\n",
    "print(\"d\" in test_str)          #True\n",
    "print(\"d\" in test_list)         #True\n",
    "print(\"d\" in test_dict)         #False\n",
    "print(\"name\" in test_dict)      #True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# not in 是否不存在（字典是查key）\n",
    "print(\"z\" not in test_str)      #True\n",
    "print(\"z\" not in test_list)     #True\n",
    "print(\"z\" not in test_dict)     #True\n",
    "print(\"name\" not in test_dict)  #False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2.内置函数扩展\n",
    "\n",
    "`len，max，min，del`\n",
    "\n",
    "len()，这个就不说了，用的太多了\n",
    "\n",
    "max()，求最大值，dict的最大值是比较的key\n",
    "\n",
    "min()，这个和max一样用，最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n",
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# len(item) 计算容器中元素个数\n",
    "print(len(test_str))\n",
    "print(len(test_list))\n",
    "print(len(test_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'w'"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# max(item) 返回容器中元素最大值\n",
    "max(test_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'>' not supported between instances of 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-149-7da4501a78c2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 这个注意一种情况（当然了，你按照之前说的规范，list里面放同一种类型就不会出错了）\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#TypeError: '>' not supported between instances of 'str' and 'int'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "# 这个注意一种情况（当然了，你按照之前说的规范，list里面放同一种类型就不会出错了）\n",
    "max(test_list) #TypeError: '>' not supported between instances of 'str' and 'int'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "wechat\n"
     ]
    }
   ],
   "source": [
    "test_list=[1,3,5,7,9,2]\n",
    "print(max(test_list))\n",
    "print(max(test_dict)) #比较key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".\n",
      "1\n",
      "name\n"
     ]
    }
   ],
   "source": [
    "# min(item) 返回容器中元素最小值\n",
    "print(min(test_str))\n",
    "print(min(test_list))\n",
    "print(min(test_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# del(item)\t        删除变量\n",
    "# del() or del xxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以忽略 cmp(item1, item2) 比较两个值\n",
    "# Python2里面有 cmp(1,2) ==> -1 \n",
    "# cmp在比较字典数据时，先比较键，再比较值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
